package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.R;
import com.example.reggie.domain.Category;
import com.example.reggie.domain.Dish;
import com.example.reggie.dto.DishDto;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.DishFlavorService;
import com.example.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.omg.CosNaming.NamingContextPackage.NotEmpty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 23333
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加菜品，同时保存对应的口味数据
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto) {
        dishService.saveDishWithDishFlavors(dishDto);
        return R.success("添加菜品成功！");
    }

    /**
     * 分页查询菜品
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        log.info(name, page, pageSize);
        Page<Dish> dishPage = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Dish::getName, name).orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage, lqw);

        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

        List<DishDto> dishDtoPageRecords = new ArrayList<DishDto>();
        List<Dish> dishPageRecords = dishPage.getRecords();

        for (Dish dishPageRecord : dishPageRecords) {
            log.info(dishPageRecord.toString());
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dishPageRecord, dishDto);
            Long categoryId = dishPageRecord.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            log.info(dishDto.toString());
            dishDtoPageRecords.add(dishDto);
        }

        dishDtoPage.setRecords(dishDtoPageRecords);

        return R.success(dishDtoPage);
    }

    /**
     * 根据id查询菜品信息和口味集合
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id) {
        DishDto dishDto = dishService.selectDishWithFlavorsById(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateDishInfo(dishDto);
        return R.success("修改菜品成功！");
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids) {
        dishService.removeById(ids);
        return R.success("删除成功！");
    }

    /**
     * 菜品切换停售启售
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable Integer status, String ids) {
        String[] splitIds = ids.split(",");
        for (String id : splitIds) {
            Dish dish = dishService.getById(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        }
        return R.success("切换成功！");
    }

    /**
     * 根据菜品条件查询菜品列表
     *
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<Dish>> listDishByCategoryId(Dish dish) {

        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();


        lqw.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId()).orderByAsc(dish.getCategoryId() != null,Dish::getSort).orderByDesc(dish.getCategoryId() != null,Dish::getUpdateTime).eq(dish.getCategoryId() != null,Dish::getStatus,1);

        lqw.like(dish.getName() != null, Dish::getName, dish.getName()).orderByAsc(dish.getName() != null,Dish::getSort).orderByDesc(dish.getName() != null,Dish::getUpdateTime).eq(dish.getName() != null,Dish::getStatus,1);

        List<Dish> dishList = dishService.list(lqw);

        return R.success(dishList);

    }


}
